package cn.vin.web;

import org.apache.commons.codec.Charsets;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.time.LocalDate;

/**
 * @Author: Wu Shenzhuang
 * @Date: 2019/5/28 15:23
 */
@RestController
@RequestMapping("/statis")
public class DownloadController {

    @RequestMapping("/download")
    public String download(String param,HttpServletResponse response) throws UnsupportedEncodingException {
        if(StringUtils.isBlank(param)) {
            param = LocalDate.now().toString();
        }
        String fileName = "埋点报表-"+ param +".xlsx";
        File file = new File(fileName);
        if(!file.exists()) {
            return "文件不存在";
        }
        response.setContentType("application/force-download");// 设置强制下载不打开
        response.setContentLengthLong(file.length());
        response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes(Charsets.UTF_8), Charsets.ISO_8859_1));
        InputStream is = null;
        BufferedInputStream bis = null;
        byte[] buffer = new byte[1024];
        try {
            is = new FileInputStream(file);
            bis = new BufferedInputStream(is);
            StreamUtils.copy(bis,response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "下载成功";
    }

    @RequestMapping("/nioDownload")
    public String nioDownload(String param,HttpServletResponse response) {
        if(StringUtils.isBlank(param)) {
            param = LocalDate.now().toString();
        }
        String fileName = "埋点报表-"+ param +".xlsx";
        File file = new File(fileName);
        if(!file.exists()) {
            return "文件不存在";
        }
        response.setContentType("application/force-download");// 设置强制下载不打开
        response.setContentLengthLong(file.length());
        response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes(Charsets.UTF_8), Charsets.ISO_8859_1));
        FileInputStream fis = null;
        FileChannel channel = null;
        ByteBuffer buff = null;
        try {
            fis = new FileInputStream(file);
            channel = fis.getChannel();
            buff = ByteBuffer.allocateDirect(786432);
            byte[] byteArr = new byte[131072];
            int nRead,nGet;
            while((nRead = channel.read(buff)) != -1) {
                if(nRead == 0) {
                    continue;
                }
                buff.position(0);
                buff.limit(nRead);
                while(buff.hasRemaining()) {
                    nGet = Math.min(buff.remaining(),131072);
                    buff.get(byteArr,0,nGet);
                    response.getOutputStream().write(byteArr);
                }
                buff.clear();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(buff != null) {
                buff.clear();
            }
            if(channel != null) {
                try {
                    channel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "下载成功";
    }
}
